package com.whu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.whu.aircraft.R;
import com.whu.aircraft.TrackInfo;
import com.whu.aircraft.User;
import com.whu.service.impl.TrackInfoService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TrackInfoController {
    final TrackInfoService trackInfoService;
    public TrackInfoController(TrackInfoService trackInfoService){
        this.trackInfoService = trackInfoService;
    }
    @RequestMapping("/track/list")
    public R list() throws JsonProcessingException {
        R r = new R();
        r.put("track_list",trackInfoService.list());
        return r;
    }

    @RequestMapping("track/listByPage/{page}")
    public R listByPage(@PathVariable int page) throws JsonProcessingException {
        IPage<TrackInfo> iPage = trackInfoService.listWithPage(page,1);
        return R.ok().put("track_list",iPage.getRecords())
                .put("pageCnt",iPage.getPages());
    }
    @RequestMapping("/track/create")
    public R create(@RequestBody TrackInfo trackInfo) throws JsonProcessingException {
//        trackInfo.list2Json();
        trackInfoService.create(trackInfo);
        return R.ok("保存成功");
    }
    @RequestMapping("/track/delete/{id}")
    public R delete(@PathVariable Long id){
        trackInfoService.delete(id);
        return R.ok("删除成功");
    }
}
